@model CaipuGuanliXitong.Controllers.DeletedItemsViewModel

@{
    ViewData["Title"] = "数据库管理";
}

<h2>数据库管理</h2>

@if (ViewBag.Message != null)
{
    <div class="alert @(ViewBag.IsSuccess ? "alert-success" : "alert-danger")">
        @ViewBag.Message
    </div>
}

<div class="row">
    <div class="col-md-6">
        <div class="card mb-4">
            <div class="card-header">
                <h4>重新创建数据库表</h4>
            </div>
            <div class="card-body">
                <p>此操作将删除所有现有的表并重新创建它们。所有数据将丢失。</p>
                <form asp-action="ReCreateDatabase" method="post">
                    @Html.AntiForgeryToken()
                    <button type="submit" class="btn btn-danger" onclick="return confirm('确定要删除并重新创建所有表吗？此操作不可撤销！')">重新创建数据库表</button>
                </form>
            </div>
        </div>
    </div>
    
    <div class="col-md-6">
        <div class="card mb-4">
            <div class="card-header">
                <h4>备份数据库</h4>
            </div>
            <div class="card-body">
                <p>创建数据库的完整备份并下载到本地，包括所有表和数据。</p>
                <form asp-action="BackupDatabase" method="post">
                    @Html.AntiForgeryToken()
                    <button type="submit" class="btn btn-primary">备份并下载数据库</button>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-6">
        <div class="card mb-4">
            <div class="card-header">
                <h4>恢复数据库</h4>
            </div>
            <div class="card-body">
                <p>从备份文件恢复数据库。这将替换当前数据。</p>
                <form asp-action="RestoreDatabase" method="post" enctype="multipart/form-data">
                    @Html.AntiForgeryToken()
                    <div class="form-group">
                        <label for="backupFile">选择备份文件:</label>
                        <input type="file" name="backupFile" class="form-control-file" accept=".zip" required />
                    </div>
                    <button type="submit" class="btn btn-warning" onclick="return confirm('确定要从备份文件恢复数据库吗？这将替换当前所有数据！')">恢复数据库</button>
                </form>
            </div>
        </div>
    </div>
</div>

<!-- 已逻辑删除的数据管理 -->
<h3>已逻辑删除的数据</h3>

<!-- 菜谱表逻辑删除数据 -->
<div class="card mb-4">
    <div class="card-header">
        <h4>菜谱表 (CookBook)</h4>
    </div>
    <div class="card-body">
        @if (Model.DeletedCookBooks.Any())
        {
            <form asp-action="PermanentlyDelete" method="post" class="mb-2">
                @Html.AntiForgeryToken()
                <input type="hidden" name="table" value="CookBook" />
                <button type="submit" class="btn btn-danger btn-sm" 
                        onclick="return confirm('确定要永久删除选中的菜谱吗？此操作不可撤销！')">
                    永久删除选中项
                </button>
                <a href="#" class="btn btn-success btn-sm" onclick="undeleteItems('CookBook')">恢复选中项</a>
                
                <div class="table-responsive mt-3">
                    <table class="table table-striped table-bordered">
                        <thead>
                            <tr>
                                <th><input type="checkbox" id="cbCookBookAll" onclick="toggleAll(this, 'CookBookIds')" /></th>
                                <th>ID</th>
                                <th>菜谱名称</th>
                                <th>分类</th>
                                <th>创建时间</th>
                                <th>更新时间</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model.DeletedCookBooks)
                            {
                                <tr>
                                    <td><input type="checkbox" name="ids" value="@item.Id" class="cb-CookBookIds" /></td>
                                    <td>@item.Id</td>
                                    <td>@item.Name</td>
                                    <td>@item.Category</td>
                                    <td>@item.CreatedTime.ToString("yyyy-MM-dd HH:mm")</td>
                                    <td>@item.UpdatedTime.ToString("yyyy-MM-dd HH:mm")</td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            </form>
        }
        else
        {
            <p>菜谱表中没有已逻辑删除的数据。</p>
        }
    </div>
</div>

<!-- 食材表逻辑删除数据 -->
<div class="card mb-4">
    <div class="card-header">
        <h4>食材表 (Ingredient)</h4>
    </div>
    <div class="card-body">
        @if (Model.DeletedIngredients.Any())
        {
            <form asp-action="PermanentlyDelete" method="post" class="mb-2">
                @Html.AntiForgeryToken()
                <input type="hidden" name="table" value="Ingredient" />
                <button type="submit" class="btn btn-danger btn-sm" 
                        onclick="return confirm('确定要永久删除选中的食材吗？此操作不可撤销！')">
                    永久删除选中项
                </button>
                <a href="#" class="btn btn-success btn-sm" onclick="undeleteItems('Ingredient')">恢复选中项</a>
                
                <div class="table-responsive mt-3">
                    <table class="table table-striped table-bordered">
                        <thead>
                            <tr>
                                <th><input type="checkbox" id="cbIngredientAll" onclick="toggleAll(this, 'IngredientIds')" /></th>
                                <th>ID</th>
                                <th>食材名称</th>
                                <th>分类</th>
                                <th>规格</th>
                                <th>单位</th>
                                <th>价格</th>
                                <th>创建时间</th>
                                <th>更新时间</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model.DeletedIngredients)
                            {
                                <tr>
                                    <td><input type="checkbox" name="ids" value="@item.Id" class="cb-IngredientIds" /></td>
                                    <td>@item.Id</td>
                                    <td>@item.Name</td>
                                    <td>@item.Category</td>
                                    <td>@item.Specification</td>
                                    <td>@item.Unit</td>
                                    <td>@item.Price.ToString("F2")</td>
                                    <td>@item.CreatedTime.ToString("yyyy-MM-dd HH:mm")</td>
                                    <td>@item.UpdatedTime.ToString("yyyy-MM-dd HH:mm")</td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            </form>
        }
        else
        {
            <p>食材表中没有已逻辑删除的数据。</p>
        }
    </div>
</div>

<!-- 菜谱食材关联表逻辑删除数据 -->
<div class="card mb-4">
    <div class="card-header">
        <h4>菜谱食材关联表 (RecipeIngredient)</h4>
    </div>
    <div class="card-body">
        @if (Model.DeletedRecipeIngredients.Any())
        {
            <form asp-action="PermanentlyDelete" method="post" class="mb-2">
                @Html.AntiForgeryToken()
                <input type="hidden" name="table" value="RecipeIngredient" />
                <button type="submit" class="btn btn-danger btn-sm" 
                        onclick="return confirm('确定要永久删除选中的菜谱食材关联吗？此操作不可撤销！')">
                    永久删除选中项
                </button>
                <a href="#" class="btn btn-success btn-sm" onclick="undeleteItems('RecipeIngredient')">恢复选中项</a>
                
                <div class="table-responsive mt-3">
                    <table class="table table-striped table-bordered">
                        <thead>
                            <tr>
                                <th><input type="checkbox" id="cbRecipeIngredientAll" onclick="toggleAll(this, 'RecipeIngredientIds')" /></th>
                                <th>ID</th>
                                <th>菜谱ID</th>
                                <th>菜谱名称</th>
                                <th>食材ID</th>
                                <th>食材名称</th>
                                <th>数量</th>
                                <th>价格</th>
                                <th>创建时间</th>
                                <th>更新时间</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model.DeletedRecipeIngredients)
                            {
                                <tr>
                                    <td><input type="checkbox" name="ids" value="@item.Id" class="cb-RecipeIngredientIds" /></td>
                                    <td>@item.Id</td>
                                    <td>@item.CookBookId</td>
                                    <td>@item.CookBookName</td>
                                    <td>@item.IngredientId</td>
                                    <td>@item.IngredientName</td>
                                    <td>@item.Quantity</td>
                                    <td>@item.Price.ToString("F2")</td>
                                    <td>@item.CreatedTime.ToString("yyyy-MM-dd HH:mm")</td>
                                    <td>@item.UpdatedTime.ToString("yyyy-MM-dd HH:mm")</td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            </form>
        }
        else
        {
            <p>菜谱食材关联表中没有已逻辑删除的数据。</p>
        }
    </div>
</div>

<!-- 周菜单表逻辑删除数据 -->
<div class="card mb-4">
    <div class="card-header">
        <h4>周菜单表 (WeeklyMenu)</h4>
    </div>
    <div class="card-body">
        @if (Model.DeletedWeeklyMenus.Any())
        {
            <form asp-action="PermanentlyDelete" method="post" class="mb-2">
                @Html.AntiForgeryToken()
                <input type="hidden" name="table" value="WeeklyMenu" />
                <button type="submit" class="btn btn-danger btn-sm" 
                        onclick="return confirm('确定要永久删除选中的周菜单吗？此操作不可撤销！')">
                    永久删除选中项
                </button>
                <a href="#" class="btn btn-success btn-sm" onclick="undeleteItems('WeeklyMenu')">恢复选中项</a>
                
                <div class="table-responsive mt-3">
                    <table class="table table-striped table-bordered">
                        <thead>
                            <tr>
                                <th><input type="checkbox" id="cbWeeklyMenuAll" onclick="toggleAll(this, 'WeeklyMenuIds')" /></th>
                                <th>ID</th>
                                <th>周开始日期</th>
                                <th>星期</th>
                                <th>餐别</th>
                                <th>性别要求</th>
                                <th>菜谱ID</th>
                                <th>菜谱名称</th>
                                <th>创建时间</th>
                                <th>更新时间</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model.DeletedWeeklyMenus)
                            {
                                <tr>
                                    <td><input type="checkbox" name="ids" value="@item.Id" class="cb-WeeklyMenuIds" /></td>
                                    <td>@item.Id</td>
                                    <td>@item.WeekStartDate.ToString("yyyy-MM-dd")</td>
                                    <td>@item.DayOfWeek</td>
                                    <td>@item.MealType</td>
                                    <td>@item.Gender</td>
                                    <td>@item.CookBookId</td>
                                    <td>@item.CookBookName</td>
                                    <td>@item.CreatedTime.ToString("yyyy-MM-dd HH:mm")</td>
                                    <td>@item.UpdatedTime.ToString("yyyy-MM-dd HH:mm")</td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            </form>
        }
        else
        {
            <p>周菜单表中没有已逻辑删除的数据。</p>
        }
    </div>
</div>

<!-- 日采购表逻辑删除数据 -->
<div class="card mb-4">
    <div class="card-header">
        <h4>日采购表 (DailyPurchase)</h4>
    </div>
    <div class="card-body">
        @if (Model.DeletedDailyPurchases.Any())
        {
            <form asp-action="PermanentlyDelete" method="post" class="mb-2">
                @Html.AntiForgeryToken()
                <input type="hidden" name="table" value="DailyPurchase" />
                <button type="submit" class="btn btn-danger btn-sm" 
                        onclick="return confirm('确定要永久删除选中的日采购记录吗？此操作不可撤销！')">
                    永久删除选中项
                </button>
                <a href="#" class="btn btn-success btn-sm" onclick="undeleteItems('DailyPurchase')">恢复选中项</a>
                
                <div class="table-responsive mt-3">
                    <table class="table table-striped table-bordered">
                        <thead>
                            <tr>
                                <th><input type="checkbox" id="cbDailyPurchaseAll" onclick="toggleAll(this, 'DailyPurchaseIds')" /></th>
                                <th>ID</th>
                                <th>日期</th>
                                <th>早餐男</th>
                                <th>早餐女</th>
                                <th>午餐男</th>
                                <th>午餐女</th>
                                <th>晚餐男</th>
                                <th>晚餐女</th>
                                <th>创建时间</th>
                                <th>更新时间</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model.DeletedDailyPurchases)
                            {
                                <tr>
                                    <td><input type="checkbox" name="ids" value="@item.Id" class="cb-DailyPurchaseIds" /></td>
                                    <td>@item.Id</td>
                                    <td>@item.Date.ToString("yyyy-MM-dd")</td>
                                    <td>@item.BreakfastMaleCount</td>
                                    <td>@item.BreakfastFemaleCount</td>
                                    <td>@item.LunchMaleCount</td>
                                    <td>@item.LunchFemaleCount</td>
                                    <td>@item.DinnerMaleCount</td>
                                    <td>@item.DinnerFemaleCount</td>
                                    <td>@item.CreatedTime.ToString("yyyy-MM-dd HH:mm")</td>
                                    <td>@item.UpdatedTime.ToString("yyyy-MM-dd HH:mm")</td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            </form>
        }
        else
        {
            <p>日采购表中没有已逻辑删除的数据。</p>
        }
    </div>
</div>

@section Scripts {
    <script>
        function toggleAll(source, className) {
            var checkboxes = document.getElementsByClassName('cb-' + className);
            for (var i = 0; i < checkboxes.length; i++) {
                checkboxes[i].checked = source.checked;
            }
        }
        
        function undeleteItems(table) {
            // 创建一个隐藏表单来提交取消删除请求
            var form = document.createElement('form');
            form.method = 'POST';
            form.action = '@Url.Action("Undelete", "Database")';
            
            // 添加防伪令牌
            var token = document.createElement('input');
            token.type = 'hidden';
            token.name = '__RequestVerificationToken';
            token.value = '@Html.AntiForgeryToken().ToString().Replace("<input name=\"__RequestVerificationToken\" type=\"hidden\" value=\"", "").Replace("\" />", "")';
            form.appendChild(token);
            
            // 添加表名
            var tableInput = document.createElement('input');
            tableInput.type = 'hidden';
            tableInput.name = 'table';
            tableInput.value = table;
            form.appendChild(tableInput);
            
            // 添加选中的ID
            var checkboxes = document.querySelectorAll('input[name="ids"]:checked');
            if (checkboxes.length === 0) {
                alert('请至少选择一项');
                return;
            }
            
            for (var i = 0; i < checkboxes.length; i++) {
                var idInput = document.createElement('input');
                idInput.type = 'hidden';
                idInput.name = 'ids';
                idInput.value = checkboxes[i].value;
                form.appendChild(idInput);
            }
            
            // 提交表单
            document.body.appendChild(form);
            form.submit();
        }
    </script>
}